from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

from shopping_mall import settings


def generate_verify_token(user_id):
    # serializer = Serializer(秘钥, 有效期 秒)
    serializer = Serializer(settings.SECRET_KEY, 3600*24*2)
    # serializer.dumps(数据), 返回bytes类型
    # print('user_id-------------', user_id)
    # print('user_id类型-------------', type(user_id))
    token = serializer.dumps({'user_id': user_id})
    # print('token---------------', token)
    token = token.decode()
    return token

def check_verify_token(token):
    # serializer = Serializer(秘钥, 有效期 秒)
    serializer = Serializer(settings.SECRET_KEY, 3600 * 24 * 2)
    try:
        user_id_dict = serializer.loads(token)
        user_id = user_id_dict.get('user_id')
        return user_id
    except:
        print('签名过期')
        return None

if __name__ == "__main__":
    token = generate_verify_token(1198)
    print(token)
